package com.zkingsoft.actions.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.ProjPost;
import com.zkingsoft.model.ProjText;
import com.zkingsoft.model.Project;
import com.zkingsoft.model.Report;
import com.zkingsoft.model.Role;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.FileDownload;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.ProjPostService;
import com.zkingsoft.services.ProjTextService;
import com.zkingsoft.services.ProjectService;
import com.zkingsoft.services.RoleService;

/**
 * @description 项目信息action
 * @author 陈才
 * @email smvic_chencai@163.com
 * @date 2016-11-09
 */
@Controller
@RequestMapping(value = "/admin/projectText")
public class ProjectTextController extends BaseController {
	@Resource
	private ProjTextService projTextService;
	@Resource
	private RoleService roleService;
	@Resource
	private ProjectService projectService;
	
	/**
	 * 角色查询 
	 */
	@RequestMapping(value = "/postNameList")
	public @ResponseBody AjaxResult postNameList() {
		Role role =new Role();
		
		//return findByModel(projPostService,projPost);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,roleService.findByModel(role),0);
	}
	/**
	 * 页面跳转
	 */
	@RequestMapping(value = "/projectText")
	public String  projectText(Long id) {
		if(id != null){
			Project project = projectService.findById(id);
			WebUtil.getRequest().setAttribute("obj",project);
		}
		return "admin/projectText-list";
	}
	/**
	 * 文档下载
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/download")
	public  @ResponseBody AjaxResult download(HttpServletResponse response,long id){
		ProjText projText = projTextService.findById(id);
		/*String url=projText.getFileUrl();
		String fileName=url.substring(url.lastIndexOf("/")+1, url.length());
		System.out.println(fileName);
	//	=projText.getFileName()+".jpg";
	//	String path=WebUtil.getResourceRealPath( "WEB-INF/views/api/demoShow/import_demo.xlsx");
	//	String path=WebUtil.getResourceRealPath(url);
		System.out.println(url);
		FileDownload.fileDownload(response, url, fileName);*/
		return new AjaxResult(AjaxResult.STATUS_OK, null, projText.getFileUrl());
	}
	
	
	
	
	/**
	 * 文档上传
	 */   
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(ProjText projText,String fileExt) {
		if (projText.getId() != null) {
			return modify(projTextService, projText, "项目文档");
		} else {
			projText.setUserId(getMe().getId());
			projText.setUploadTime(new Date());
			//文件名加上扩展名
			if(fileExt!=null && !fileExt.equals("")){
			projText.setFileName(projText.getFileName()+"."+fileExt);
			}
			return add(projTextService, projText, "项目文档");
		}
	}
	/*@RequestMapping(value = "/uploadFile")
	public String uploadFile(ProjText projText){
		
		projTextService.add(projText);
		return "redirect:/admin/projectText/projectText";
		
	}*/
	
	
	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(projTextService, keys);
	}
	
	/**
	 * 文档查询
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(ProjText projText,PaginationVO pageVo) {
		if(getMe().getRoleId() != null){
			projText.setRoleIds(getMe().getRoleId().toString());
		}
		projText.setUserId(getMe().getId());
		//return new AjaxResult(AjaxResult.STATUS_OK, null,null,projTextService.findByModel(projText),0);
		return showList(projTextService, projText, pageVo);
	}
	
	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long projId,Long id) {
		if(projId != null ){
			WebUtil.getRequest().setAttribute("obj",projectService.findById(projId) );
		}
		if(id != null){
			WebUtil.getRequest().setAttribute("projText", projTextService.findById(id));
		}
		return "admin/projectText-form";
	}


}
